package com.example.mqdemo.rabbitmq.topic.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicConfig {

    //初始化交换机
    @Bean
    TopicExchange topicExchange(){
        return new TopicExchange("topic",true,false);
    }
    //初始化队列
    @Bean
    Queue xiaomi(){
        return new Queue("xiaomi");
    }
    @Bean
    Queue huawei(){
        return new Queue("huawei");
    }
    @Bean
    Queue phone(){
        return new Queue("phone");
    }
    //队列与交换机绑定
    @Bean
    Binding xiaomi1(){
        return BindingBuilder.bind(xiaomi()).to(topicExchange()).with("xiaomi.#");
    }
    @Bean
    Binding huawie(){
        return BindingBuilder.bind(huawei()).to(topicExchange()).with("huawei.#");
    }
    @Bean
    Binding phone1(){
        return BindingBuilder.bind(phone()).to(topicExchange()).with("#.phone.#");
    }

    /**
     * routingKey为XXX.#表示消息的routingKey以xxx开头的都将被路由到名称为 “xxx” 的 Queue 上
     */
}
